﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;

namespace Runda.CMS.Dal
{
    public partial class CtrtContractGiftDal
    {
        /// <summary>
        /// 获取合同配套商品
        /// </summary>
        /// <param name="ContractID"></param>
        /// <returns></returns>
        public DataTable LoadGiftByContractID(Guid ContractID)
        {
            string sql = @"select A.GiftID,A.CreatedAt,A.Cost,B.InstrumentCode,B.InstrumentName,B.instrumentStandard,B.Unit from Cms_Ctrt_ContractGift A join Cms_Ctrt_Instrument B on A.InstrumentID = B.InstrumentGUID where A.ContractID = @ContractID";

            DbCommand dbcommand = CurrentDatabase.GetSqlStringCommand(sql);
            CurrentDatabase.AddInParameter(dbcommand, "@ContractID", DbType.Guid, ContractID);
            return CurrentDatabase.ExecuteDataSet(dbcommand).Tables[0];
        }

        /// <summary>
        /// 获取配套商品的成本
        /// </summary>
        /// <param name="ContractID"></param>
        /// <returns></returns>
        public decimal GetContractGiftCost(Guid ContractID)
        {
            string sql = "select isnull(sum(B.Cost),0) from Cms_Ctrt_ContractGift A join Cms_Ctrt_Instrument B on A.InstrumentID = B.InstrumentGUID where A.ContractID = @ContractID";
            DbCommand dbcommand = CurrentDatabase.GetSqlStringCommand(sql);
            CurrentDatabase.AddInParameter(dbcommand, "@ContractID", DbType.Guid, ContractID);
            object obj = CurrentDatabase.ExecuteScalar(dbcommand);
            if (obj != null)
                return (decimal)obj;
            else
                return 0;
        }
    }
}
